package com.bus.controller;

import com.bus.entity.Goods;
import com.bus.service.GoodsService;
import com.bus.service.TaskService;
import com.bus.vo.TaskVo;
import com.common.AppFileUtils;
import com.common.DataGridView;
import com.bus.vo.GoodsVo;
import com.common.ResultObj;
import com.constast.SysConstast;
import com.study.entity.PurChase;
import com.study.entity.PurChaseVo;
import com.study.service.PurChaseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/bus/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @Resource
    private PurChaseService purChaseService;

    @Resource
    TaskService taskService;
    /**
     * 分页查询、模糊查询商品信息
     * @param goodsVo
     * @return
     */
    @RequestMapping("/loadGoods")
    @ResponseBody
    public DataGridView loadGoods(GoodsVo goodsVo) {
        return goodsService.selectAllGoods(goodsVo);
    }

    /**
     * 请求转发访问jsp
     * @return
     */
    @RequestMapping(value = "/goodsManagement",method = RequestMethod.GET)
    public String showGoods() {
        return "forward:/view/resp/goodsManager.jsp";
    }

    /**
     * 增加商品
     * @param goodsVo
     * @return
     */
    @RequestMapping("/addGoods")
    @ResponseBody
    public ResultObj addGoods(GoodsVo goodsVo) {
        try {
            //获取图片地址,判断是否是临时文件
            String goodsImg = goodsVo.getGoodsImg();
            if (goodsImg.endsWith(SysConstast.UPLOAD_FILE_TEMPSUFFIX)) {
                // 临时文件转正
                goodsImg = AppFileUtils.updateFileName(goodsImg, SysConstast.UPLOAD_FILE_TEMPSUFFIX);
                // 转正文件地址更新
                goodsVo.setGoodsImg(goodsImg);
            }
            //添加商品
            goodsService.save(goodsVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    /**
     * 修改商品
     * @param goodsVo
     * @return
     */
    @RequestMapping("/updateGoods")
    @ResponseBody
    public ResultObj updateGoods(GoodsVo goodsVo) {
        try {
            //获取图片地址,判断是否是临时文件
            String goodsImg = goodsVo.getGoodsImg();
            if (goodsImg.endsWith(SysConstast.UPLOAD_FILE_TEMPSUFFIX)) {
                //临时文件转正
                goodsImg = AppFileUtils.updateFileName(goodsImg, SysConstast.UPLOAD_FILE_TEMPSUFFIX);
                //转正文件地址更新
                goodsVo.setGoodsImg(AppFileUtils.DIR_PATH + goodsImg);
                // 删除原有的图片
                String oldGoodsImg = goodsService.getById(goodsVo.getGoodsId()).getGoodsImg();
                AppFileUtils.deleteFileUsePath(oldGoodsImg.replace(AppFileUtils.DIR_PATH,""));
                System.out.println(oldGoodsImg);
            }
            //更新商品
            goodsService.updateById(goodsVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }

    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @RequestMapping("/deleteGoods")
    @ResponseBody
    public ResultObj deleteGoods(Integer goodsId) {
        try {
            goodsService.deleteById(goodsId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }

    @RequestMapping("/deleteBatchGoods")
    @ResponseBody
    public ResultObj deleteBatchCustomer(Integer[] ids){
        try {
            goodsService.deleteByIds(Arrays.asList(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }

    @RequestMapping("/loadGoodsOfProvider/{providerId}")
    @ResponseBody
    public List<Goods> loadGoodsOfProvider(@PathVariable Integer providerId) {
        return goodsService.selectGoodsById(providerId);
    }

    @RequestMapping("/getGoods")
    @ResponseBody
    public List<Goods> getGoods(Integer id){
        return goodsService.selectGoods();
    }

    @RequestMapping(value = "/goodsManagementInport",method = RequestMethod.GET)
    public String showGoodsInport() {
        return "forward:/view/data/goodsManagerInport.jsp";
    }

    @RequestMapping("/updateGoodsIn")
    @ResponseBody
    public ResultObj updateGoods(PurChaseVo purChaseVo){
        try{
            System.out.println(purChaseVo.getId());
            goodsService.updateNum(purChaseVo.getGoodsId(),purChaseVo.getGoodsNum());
            purChaseService.update(purChaseVo.getId());
            TaskVo task=new TaskVo();
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.PURIN_ERROR;
        }
        return ResultObj.PURIN_SUCCESS;
    }

    @RequestMapping("/CannotUpdateGoodsIn")
    @ResponseBody
    public ResultObj CannotUpdateGoodsIn(PurChaseVo purChaseVo){
        try{
            TaskVo task=new TaskVo();
            taskService.Delete(purChaseVo.getId());
            task.setId(purChaseVo.getId());
            task.setTaskName(purChaseVo.getGoodsName()+"商品拒绝入库");
            task.setParentId(5);
            task.setHref("bus/inport/inportManagement");
            task.setTaskType("商品拒绝入库");
            taskService.Insert(task);
            purChaseService.CannotUpdate(purChaseVo.getId());
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.NOTIN_ERROR;
        }
        return ResultObj.NOTIN_SUCCESS;
    }

    @RequestMapping(value = "/goodsManagementOutport",method = RequestMethod.GET)
    public String showGoodsOutport() {
        return "forward:/view/data/goodsManagerOutport.jsp";
    }
}
